package com.hzhc.controller.wxapp;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzhc.annotation.SysTemLog;
import com.hzhc.common.BaseController;
import com.hzhc.dto.UserDto;
import com.hzhc.entity.CntrInfo;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.enumBean.TSXEnum;
import com.hzhc.service.*;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.TokenUtil;
import com.hzhc.vo.CntrInfoVo;
import com.hzhc.vo.CntrTallyVo;
import com.hzhc.vo.DeclareCarVo;
import com.hzhc.vo.VehicleReservationVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author wuxiaoke
 * @description app提箱，返场理货控制器
 * @date 2024/6/15 下午4:26
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/app")
public class WxApiCntrTSXTallyController extends BaseController {
    private final DeclareCarService declareCarService;
    private final CntrInfoService cntrInfoService;
    private final TrainPlanDetailService trainPlanDetailService;
    private final VehicleReservationService vehicleReservationService;
    private final UserMemberService memberService;

    /**
     * 查询卡口数据
     */
    @RequestMapping("/getGateCarList")
    public ResultUtil<List<DeclareCarVo>> getGateCarList(@RequestBody DeclareCarVo vo) {
        Page<DeclareCarVo> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        List<DeclareCarVo> declareCarVos = declareCarService.getZkTodayRcCarList(page, vo);
        return ResultUtil.build(declareCarVos, page.getTotal(), page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 查询统计卡口进卡TX,SX,ZC,XC待理货数据
     * @return
     */
    @RequestMapping("/getCountCntrTallyNumber")
    public ResultUtil<JSONObject> getCountCntrTallyNumber(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("TX",declareCarService.getCountTodayRcCarList(TSXEnum.TX.getCode()));
        jsonObject.put("ZJ",declareCarService.getCountTodayRcCarList(TSXEnum.SX.getCode()));
        jsonObject.put("ZC",trainPlanDetailService.getCountZxcTallyNumber(TSXEnum.ZC.getCode()));
        jsonObject.put("XC",trainPlanDetailService.getCountZxcTallyNumber(TSXEnum.XC.getCode()));

        Long userID = TokenUtil.getUserID(request);
        String type = memberService.getById(userID).getUserType();
        VehicleReservationVo vo = new VehicleReservationVo();
        Page<VehicleReservationVo> page =new Page<>(1,999);
        vo.setSubmitType(0);
        vo.setMemberId(userID);
        List<VehicleReservationVo> list = null;
        if ("1".equals(type)){//货主
            list = vehicleReservationService.getWxList(page, vo);
        }else if ("2".equals(type)){//司机
            list = vehicleReservationService.getDriverList(page, vo);
        }

        jsonObject.put("YY",list.size()>0 ? list.size():0);
        return ResultUtil.build(jsonObject, ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 中控理货
     */
    @PostMapping("/cntrDuiTally")
    public ResultUtil<T> cntrDuiTally(HttpServletRequest request,@RequestBody CntrTallyVo vo) {
        UserDto userMember = TokenUtil.getUserMember(request);
        vo.setMid(userMember.getId());
        vo.setUserName(userMember.getRealname());
        vo.setAccesspath("APP端");
        return  cntrInfoService.cntrDuiTally(vo);
    }

    /**
     * 提箱理货
     */
    @PostMapping("/cntrTxTally")
    public ResultUtil<T> cntrTxTally(HttpServletRequest request, @RequestBody DeclareCarVo vo) {
        UserDto userMember = TokenUtil.getUserMember(request);
        vo.setMemberId(userMember.getId());
        vo.setUserName(userMember.getRealname());
        vo.setAccesspath("APP端");
        return  declareCarService.updateCntrTxTally(vo);
    }

    /**
     * 选择在场箱数据
     */
    @RequestMapping("/getCntrList")
    public ResultUtil<List<CntrInfoVo>> getCntrList(@RequestBody CntrInfoVo vo) {
        Page<CntrInfoVo> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        List<CntrInfoVo> cntrInfoVoList = cntrInfoService.getAllList(page, vo);
        return ResultUtil.build(cntrInfoVoList,page.getTotal(),page.getPages(),ResultUtilCodeEnum.SUCCESS);
    }


    /**
     * 查询在场箱，上层的箱子
     */
    @RequestMapping("/getOnRoadList")
    public ResultUtil<List<CntrInfo>> getOnRoadList(@RequestBody CntrInfoVo vo) {
        List<CntrInfo> cntrInfoVoList = cntrInfoService.getOnRoadList(vo);
        return ResultUtil.build(cntrInfoVoList,ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 移箱操作
     * @param cntrTallyVo
     * @return
     */
    @RequestMapping("/cntrMoveBay")
    @SysTemLog(operModul = "移箱", operType = "移箱", operDesc = "移箱功能", modular = "APP")
    public ResultUtil<T> cntrMoveBay(HttpServletRequest request, @RequestBody CntrTallyVo cntrTallyVo) {
        UserDto userMember = TokenUtil.getUserMember(request);
        cntrTallyVo.setMid(userMember.getId());
        cntrTallyVo.setUserName(userMember.getRealname());
        cntrTallyVo.setAccesspath("APP端");
        return cntrInfoService.cntrMoveBay(cntrTallyVo);
    }

}
